package tutorial.java.swing;

import java.awt.AWTException;
import java.awt.Image;
import java.awt.MenuItem;
import java.awt.SystemTray;
import java.awt.Toolkit;
import java.awt.TrayIcon;
import java.awt.PopupMenu;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JOptionPane;

public class SystemTrayDemo {
	public static void main(String[] args){
		if (!SystemTray.isSupported()) {
			JOptionPane.showMessageDialog(null, "SystemTray not support");
			return;
		}
		
		SystemTray systemTray = SystemTray.getSystemTray();
		Toolkit toolkit = Toolkit.getDefaultToolkit();
		Image image = toolkit.getImage("C:/images.jpg");
		
		PopupMenu menu = new PopupMenu();
		MenuItem action = new MenuItem("Action");
		action.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				JOptionPane.showMessageDialog(null, "Show action menu");
			}
		});
		
		MenuItem exit = new MenuItem("Exit");
		exit.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				System.exit(0);
			}
		});
		
		menu.add(action);
		menu.add(exit);
		
		TrayIcon trayIcon = new TrayIcon(image, "SystemTray Demo", menu);
		trayIcon.setImageAutoSize(true);
		try {
			systemTray.add(trayIcon);
		} catch (AWTException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
}
